package viper.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;

import viper.main.MySQLFunctions;
import viper.main.StateManager;
import viper.main.ViperHeader;

/**
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>JPanel for when the user is in a call.  Displays who you're talking to
 * and the relays being used.
 */
public class InCallPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
    public InCallPanel() {
        initComponents();
    }
    
    private JPanel leftPanel = new JPanel();
    private JPanel rightPanel = new JPanel();

    private JButton endCallButton; //End call button
    private JLabel talkingToLabel = new JLabel("In call with: ");
    private JLabel calleeLabel = new JLabel();
    private JLabel inCallLabel = new JLabel("using : ");
    private JTextArea jTextArea; //Text area to display callee and relays
    private DefaultListModel listModel = new DefaultListModel();
    private JList list = new JList(listModel);
    private JScrollPane scrollPane = new JScrollPane();
    
    //Statistics part
	private JLabel statLabel1 = new JLabel();
	private JLabel statLabel2 = new JLabel();
	private JLabel statLabel3 = new JLabel();
	private JLabel statLabel4 = new JLabel();

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
    	
    	//box layout
    	setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    	
    	leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
    	leftPanel.setBackground(new Color(255, 255, 153));
    	leftPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    	
    	rightPanel.setPreferredSize(new Dimension(600, 600));
    	rightPanel.setLayout(new BorderLayout());
    	rightPanel.setBackground(Color.WHITE);
    	rightPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

        endCallButton = new JButton("End Call");
        
        talkingToLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        inCallLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        talkingToLabel.setBackground(Color.WHITE);
        inCallLabel.setBackground(new Color(255, 255, 153));
        
        jTextArea = new JTextArea();
        jTextArea.setEditable(false);

        endCallButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endCallButtonActionPerformed(evt);
            }
        });
        
        //Relays info
        JPanel relayPane = new JPanel();
        relayPane.setLayout(new BorderLayout());
        relayPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
        relayPane.setBackground(new Color(255, 255, 153));
    	list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setCellRenderer(new IconListRenderer());
		scrollPane.setViewportView(list);
		relayPane.add(Box.createVerticalGlue());
		relayPane.add(scrollPane, BorderLayout.CENTER);

        //Callee info
        JPanel calleePane = new JPanel();
        calleePane.setMaximumSize(new Dimension(300, 200));
        JPanel calleeSubPane = new JPanel();
        calleePane.setBackground(Color.WHITE);
        calleePane.setLayout(new BorderLayout());
        calleeSubPane.setLayout(new BoxLayout(calleeSubPane, BoxLayout.Y_AXIS));
        calleeSubPane.setBackground(Color.WHITE);
        calleeSubPane.add(Box.createVerticalGlue());
        calleeSubPane.add(talkingToLabel, BorderLayout.PAGE_START);
        calleeSubPane.add(Box.createRigidArea(new Dimension(0, 10)));
        calleeLabel.setBackground(Color.WHITE);
        calleeSubPane.add(calleeLabel, BorderLayout.CENTER);
        calleeSubPane.add(Box.createRigidArea(new Dimension(0, 10)));
        calleeSubPane.add(inCallLabel);
        calleeSubPane.add(Box.createRigidArea(new Dimension(0, 10)));
        calleeSubPane.add(relayPane);
        calleePane.add(calleeSubPane, BorderLayout.PAGE_START);
     
        //End call
        JPanel buttonPane = new JPanel();
        buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
        buttonPane.setBackground(new Color(255, 255, 153));
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(endCallButton, BorderLayout.AFTER_LAST_LINE);
        
        //Graph for right side
        JPanel graphPane = new JPanel();
        graphPane.setPreferredSize(new Dimension(500, 400));
        graphPane.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        
        //Stats for right side
        JPanel statsPane = new JPanel();
        statLabel1.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        statLabel2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        statLabel3.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        statLabel4.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        GridLayout gridLayout2 = new GridLayout(4, 1);        
        gridLayout2.setVgap(5);
		statsPane.setLayout(gridLayout2);
        statsPane.setBackground(Color.WHITE);
        statsPane.add(statLabel1);
        statsPane.add(statLabel2);
        statsPane.add(statLabel3);
        statsPane.add(statLabel4);
        
        leftPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        leftPanel.add(calleePane, BorderLayout.NORTH);
        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(relayPane);
        leftPanel.add(buttonPane);
        leftPanel.add(Box.createVerticalGlue());
        
        rightPanel.add(graphPane, BorderLayout.PAGE_START);
        rightPanel.add(statsPane, BorderLayout.PAGE_END);
    	
    	add(Box.createHorizontalGlue());
        add(leftPanel);
        add(Box.createRigidArea(new Dimension(10, 0)));
        add(rightPanel, BorderLayout.LINE_END);
    }

    /**
     * @param evt
     * End call button pressed.
     */
    private void endCallButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        StateManager.INSTANCE.endCall();
    }                          
    
    /**
     * Updates the display to show the relays in use.
     */
    public void update(){
    	ArrayList<Object[]> relays = StateManager.INSTANCE.getRelays();
    	byte divCode = StateManager.INSTANCE.getDiversityCode();
    	
    	String divCodeStr = null;
    	
    	if(divCode==ViperHeader.CODE_111)
    		divCodeStr = "(1,1,1)";
    	else if(divCode==ViperHeader.CODE_232)
    		divCodeStr = "(2,3,2)";
    	else if(divCode==ViperHeader.CODE_364)
    		divCodeStr = "(3,6,4)";
    	else
    		divCodeStr = "(1,0,1)";
    	
    	String relaysString = "";
    	if(!relays.isEmpty())
    	{
    		for(Object[] relay : relays)
    		{
    			relaysString+=relay[1] + "\n";
    		}
    	}
//    	jTextArea.setText("In call w/ " + StateManager.INSTANCE.getCalleeArray()[1] +
//    			" using code " + divCodeStr + 
//    			" and Relays: \n" + relaysString
//    			);
    	
    	String calleeName = (String)StateManager.INSTANCE.getCalleeArray()[1];
    	calleeLabel.setText(calleeName);
    	calleeLabel.setIcon(new ImageIcon("images/green_phone.gif"));
    	//calleeLabel.setBackground(Color.WHITE);
    	calleeLabel.setBackground(new Color(255, 255, 153));
    	calleeLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
    	calleeLabel.repaint();
    	
    	leftPanel.repaint();
    	
    	inCallLabel.setText("using " + divCodeStr + " diversity:");
    	
    	ImageIcon icon = null;
		listModel.clear();
		for(Object[] peer : relays)
		{
			String peerString = peer[1] + "---" + peer[0];
			if(peer[2].equals(MySQLFunctions.STATUS_FREE))
			{
				icon = new ImageIcon("images/green_phone.gif");
			}
			else if(peer[2].equals(MySQLFunctions.STATUS_BUSY))
			{
				icon = new ImageIcon("images/busy.gif");
			}
			else if(peer[2].equals(MySQLFunctions.STATUS_BAD_RELAY))
			{
				icon = new ImageIcon("images/relay.gif");
			}
			JLabel label = new JLabel(peerString);
			label.setIcon(icon);
			listModel.addElement(label);
		}
    	list.repaint();
    	
    	updateStatsInFrame();
    	
    	ViperGUI.INSTANCE.repaint();
    }
	
	public void updateStatsInFrame() {
		long[] stats = StateManager.INSTANCE.getCallReceiver().getStatistics();
		
		long stat1 = stats[0];
		long stat2 = stats[1];
		long stat3 = stats[2];
		long stat4 = stats[3];
		
		statLabel1.setText(" Total Received Packets: " + stat1);
		statLabel2.setText(" Total Sent Packets: " + stat2);
		statLabel3.setText(" Total Received Voice Packets: " + stat3);
		statLabel4.setText(" Total Sent Voice Packets: " + stat4);
		statLabel1.repaint();
		statLabel2.repaint();
		statLabel3.repaint();
		statLabel4.repaint();
	}
}
